package com.pawelthiel.persistence.dao.implementation;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import com.framework.exceptions.NotImplementedException;
import com.framework.persistence.jpa.AbstractGenericJpaDAO;
import com.framework.services.ServiceFacade;
import com.pawelthiel.persistence.dao.IDataAccessObject;
import com.pawelthiel.persistence.entity.AbstractEntity;
import com.pawelthiel.persistence.entity.User;
import com.pawelthiel.persistence.mapper.EntityToTransferObjectMapper;
import com.pawelthiel.persistence.mapper.TransferObjectToEntityMapper;
import com.pawelthiel.persistence.to.AbstractTO;
import com.pawelthiel.persistence.to.UserTO;
import com.pawelthiel.persistence.to.BenutzerrolleTO;

public class UserDAO extends AbstractGenericJpaDAO<User, String> implements IDataAccessObject<UserTO> {

	@Override
	public AbstractTO select(int pk) throws PersistenceException,
			com.framework.exceptions.PersistenceException {
		throw new NotImplementedException();
	}

	@Override
	public UserTO findByFilter(int key) {
		throw new NotImplementedException();
	}

	@Override
	public UserTO findByFilter(String username) {
		User user = null;
		UserTO userTO = new UserTO();
		try {
			user = findByPrimaryKey(username);
			userTO = EntityToTransferObjectMapper.mapUser(user);
			
			BenutzerrolleTO bTo = new BenutzerrolleTO();
			
			IDataAccessObject bDao = ServiceFacade.getService(IDataAccessObject.class, bTo);
			bTo = (BenutzerrolleTO) bDao.findByFilter(userTO.getId());
			
			userTO.setRolle(bTo);
			
		} catch (com.framework.exceptions.PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return userTO;
	}

	@Override
	public Integer getPrimaryKey(AbstractEntity entity) {
		throw new NotImplementedException();
	}

	@Override
	public List<UserTO> selectAll() {
		List<UserTO> users = new ArrayList<UserTO>();
		Query query = createNativeQuery("select u from User u");
		List<User> resultList = query.getResultList();
		
		for(User user : resultList) {
			UserTO userTO = EntityToTransferObjectMapper.mapUser(user);
			users.add(userTO);
		}
		return users;
	}

	@Override
	public void insert(AbstractTO to)
			throws com.framework.exceptions.PersistenceException {
		UserTO userTO = (UserTO) to;
		User user = new User();
		
		TransferObjectToEntityMapper.mapUser(user, userTO);
		
		IDataAccessObject<BenutzerrolleTO> bDao = ServiceFacade.getService(IDataAccessObject.class, userTO.getRolle());
		BenutzerrolleTO rolle = bDao.findByFilter(userTO.getId());
		
		create(user);

	}

	@Override
	public void update(AbstractTO to)
			throws com.framework.exceptions.PersistenceException {
		UserTO userTO = (UserTO) to;
		User user = findByPrimaryKey(userTO.getId());
		
		TransferObjectToEntityMapper.mapUser(user, userTO);
		
		save(user);
	}

	@Override
	public void delete(AbstractTO to)
			throws com.framework.exceptions.PersistenceException {
		// TODO Auto-generated method stub

	}

}
